package com.example.dense_rack_server.controller;


import com.example.dense_rack_server.entity.MyRole;
import com.example.dense_rack_server.entityVo.MenuALLVo;
import com.example.dense_rack_server.entityVo.RoleDto;
import com.example.dense_rack_server.service.MyRoleService;
import com.example.dense_rack_server.utils.ResponseModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-03
 */
@Controller
@RequestMapping("/my/role")
public class MyRoleController {

    @Autowired
    private MyRoleService myRoleService;

    @GetMapping("/index")
    public String index(){
        return "role/role-list";
    }
    @ResponseBody
    @GetMapping("/list")
    public Map<String,Object> roleList(@RequestParam(value = "roleName",required = false) String roleName,
                                       @RequestParam(value = "page") int page,
                                       @RequestParam(value = "limit") int limit){
        return myRoleService.roleList(roleName,page,limit);
    }

    /**
     * 角色添加页面
     * @return
     */
    @GetMapping("/add/view")
    public ModelAndView roleAdd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("MyRole",new MyRole());
        modelAndView.setViewName("role/role-add");
        return modelAndView;
    }
    /**
     * 角色保存
     * @return
     */
    @PostMapping("/add/save")
    @ResponseBody
    public ResponseModel roleAdd(@RequestBody RoleDto roleDto){
        return myRoleService.save(roleDto);
    }

    @GetMapping("/build")
    @ResponseBody
    public ResponseModel buildMenuAll(){
        List<MenuALLVo> menuAll = myRoleService.buildMenuAll();
        return ResponseModel.success(menuAll);
    }

    /**
     * 角色编辑页面
     * @param roleId
     * @return
     */
    @GetMapping("/edit/view")
    public ModelAndView editView(@RequestParam int roleId){
        MyRole myRole = myRoleService.getRoleById(roleId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("role/role-edit");
        modelAndView.addObject("MyRole",myRole);
        return modelAndView;
    }

    /**
     * 角色编辑保存
     * @param roleDto
     * @return
     */
    @PostMapping("/edit/save")
    @ResponseBody
    public ResponseModel editSave(@RequestBody RoleDto roleDto){
        return myRoleService.editSave(roleDto);
    }

    /**
     * 角色修改使用
     * @param roleId
     * @return
     */
    @GetMapping("/ebuild")
    @ResponseBody
    @ApiOperation(value = "通过id绘制菜单树")
    public ResponseModel buildMenuAllByRoleId(@RequestParam Integer roleId,@RequestParam(value = "disabled",required = true) Boolean disabled){
        List<MenuALLVo> menuAll = myRoleService.buildMenuAllByRoleId(roleId,disabled);
        return ResponseModel.success(menuAll);
    }

    /**
     * 角色删除
     * @param roleId
     * @return
     */
    @DeleteMapping("/remove/{roleId}")
    @ResponseBody
    public ResponseModel removeRole(@PathVariable("roleId") String roleId){
        return myRoleService.removeRole(roleId);
    }

    /**
     * 角色详情
     */
    @GetMapping("/detail/{roleId}")
    public ModelAndView roleDetail(@PathVariable("roleId") String roleId) {
        return myRoleService.roleDetail(roleId);
    }

    @GetMapping("/all")
    @ResponseBody
    public ResponseModel all() {
        List<Map<String, Object>> myRoleList = myRoleService.listMaps();
        return ResponseModel.success(myRoleList);
    }




}
